import pygame
import random
import sys
import math

# 初始化pygame
pygame.init()

# 游戏设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60

# 地狱主题颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARK_RED = (139, 0, 0)
FIRE_ORANGE = (255, 69, 0)
HELL_RED = (220, 20, 60)
DARK_ORANGE = (255, 140, 0)
YELLOW = (255, 255, 0)
DARK_GRAY = (64, 64, 64)
LIGHT_GRAY = (192, 192, 192)
BROWN = (139, 69, 19)
DARK_BROWN = (101, 67, 33)

class Particle:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vx = random.uniform(-2, 2)
        self.vy = random.uniform(-3, -1)
        self.life = 30
        self.max_life = 30
        
    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.1
        self.life -= 1
        
    def draw(self, screen):
        alpha = int(255 * (self.life / self.max_life))
        color = (255, alpha, 0)
        pygame.draw.circle(screen, color, (int(self.x), int(self.y)), 2)

class Bird:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.velocity = 0
        self.gravity = 0.5
        self.jump_strength = -8
        self.size = 25
        self.wing_angle = 0
        self.particles = []
        
    def jump(self):
        self.velocity = self.jump_strength
        # 添加跳跃粒子效果
        for _ in range(5):
            self.particles.append(Particle(self.x, self.y + 10))
        
    def update(self):
        self.velocity += self.gravity
        self.y += self.velocity
        self.wing_angle += 0.3
        
        # 更新粒子
        for particle in self.particles[:]:
            particle.update()
            if particle.life <= 0:
                self.particles.remove(particle)
        
    def draw(self, screen):
        # 绘制地狱恶魔鸟
        center_x, center_y = int(self.x), int(self.y)
        
        # 身体（椭圆形）
        body_rect = pygame.Rect(center_x - 15, center_y - 10, 30, 20)
        pygame.draw.ellipse(screen, DARK_RED, body_rect)
        
        # 头部
        head_rect = pygame.Rect(center_x - 8, center_y - 20, 16, 16)
        pygame.draw.ellipse(screen, HELL_RED, head_rect)
        
        # 眼睛（发光的红色）
        eye1_pos = (center_x - 5, center_y - 15)
        eye2_pos = (center_x + 5, center_y - 15)
        pygame.draw.circle(screen, FIRE_ORANGE, eye1_pos, 4)
        pygame.draw.circle(screen, FIRE_ORANGE, eye2_pos, 4)
        pygame.draw.circle(screen, WHITE, eye1_pos, 2)
        pygame.draw.circle(screen, WHITE, eye2_pos, 2)
        
        # 翅膀（动态摆动）
        wing_offset = int(5 * math.sin(self.wing_angle))
        wing_points = [
            (center_x - 10, center_y + wing_offset),
            (center_x - 25, center_y + 5 + wing_offset),
            (center_x - 15, center_y + 10 + wing_offset)
        ]
        pygame.draw.polygon(screen, DARK_ORANGE, wing_points)
        
        # 尾巴
        tail_points = [
            (center_x + 15, center_y),
            (center_x + 25, center_y - 5),
            (center_x + 20, center_y + 5)
        ]
        pygame.draw.polygon(screen, DARK_RED, tail_points)
        
        # 小角
        horn_points = [
            (center_x - 2, center_y - 25),
            (center_x - 5, center_y - 30),
            (center_x + 2, center_y - 25)
        ]
        pygame.draw.polygon(screen, DARK_GRAY, horn_points)
        
        # 绘制粒子效果
        for particle in self.particles:
            particle.draw(screen)
        
    def get_rect(self):
        return pygame.Rect(self.x - self.size, self.y - self.size, self.size * 2, self.size * 2)

class Pipe:
    def __init__(self, x):
        self.x = x
        self.width = 60
        self.gap = 150
        self.top_height = random.randint(100, SCREEN_HEIGHT - self.gap - 100)
        self.bottom_y = self.top_height + self.gap
        self.speed = 3
        self.passed = False
        
    def update(self):
        self.x -= self.speed
        
    def draw(self, screen):
        # 绘制地狱风格的障碍物 - 熔岩柱
        # 上熔岩柱
        top_rect = pygame.Rect(self.x, 0, self.width, self.top_height)
        pygame.draw.rect(screen, DARK_RED, top_rect)
        pygame.draw.rect(screen, HELL_RED, (self.x + 5, 0, self.width - 10, self.top_height))
        
        # 下熔岩柱
        bottom_rect = pygame.Rect(self.x, self.bottom_y, self.width, SCREEN_HEIGHT - self.bottom_y)
        pygame.draw.rect(screen, DARK_RED, bottom_rect)
        pygame.draw.rect(screen, HELL_RED, (self.x + 5, self.bottom_y, self.width - 10, SCREEN_HEIGHT - self.bottom_y))
        
        # 添加熔岩纹理
        for i in range(0, self.top_height, 20):
            pygame.draw.line(screen, FIRE_ORANGE, (self.x, i), (self.x + self.width, i), 2)
        for i in range(self.bottom_y, SCREEN_HEIGHT, 20):
            pygame.draw.line(screen, FIRE_ORANGE, (self.x, i), (self.x + self.width, i), 2)
        
    def get_top_rect(self):
        return pygame.Rect(self.x, 0, self.width, self.top_height)
        
    def get_bottom_rect(self):
        return pygame.Rect(self.x, self.bottom_y, self.width, SCREEN_HEIGHT - self.bottom_y)
        
    def is_off_screen(self):
        return self.x + self.width < 0

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("Hell Flappy Bird")
        self.clock = pygame.time.Clock()
        self.game_state = "start"  # start, playing, game_over
        self.reset_game()
        
    def reset_game(self):
        self.bird = Bird(100, SCREEN_HEIGHT // 2)
        self.pipes = []
        self.score = 0
        self.game_over = False
        self.font = pygame.font.Font(None, 36)
        self.big_font = pygame.font.Font(None, 72)
        self.background_particles = []
        
        # 初始化背景粒子
        for _ in range(20):
            self.background_particles.append({
                'x': random.randint(0, SCREEN_WIDTH),
                'y': random.randint(0, SCREEN_HEIGHT),
                'size': random.randint(1, 3),
                'speed': random.uniform(0.5, 2)
            })
        
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                    if self.game_state == "start":
                        self.game_state = "playing"
                    elif self.game_state == "game_over":
                        self.game_state = "start"
                        self.reset_game()
                    elif self.game_state == "playing":
                        self.bird.jump()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.game_state == "start":
                    self.game_state = "playing"
                elif self.game_state == "game_over":
                    self.game_state = "start"
                    self.reset_game()
        return True
        
    def update(self):
        # 更新背景粒子
        for particle in self.background_particles:
            particle['x'] -= particle['speed']
            if particle['x'] < -10:
                particle['x'] = SCREEN_WIDTH + 10
                particle['y'] = random.randint(0, SCREEN_HEIGHT)
        
        if self.game_state == "playing":
            self.bird.update()
            
            # 检查小鸟是否撞地或撞天花板
            if self.bird.y > SCREEN_HEIGHT or self.bird.y < 0:
                self.game_state = "game_over"
                
            # 生成新管道
            if len(self.pipes) == 0 or self.pipes[-1].x < SCREEN_WIDTH - 200:
                self.pipes.append(Pipe(SCREEN_WIDTH))
                
            # 更新管道
            for pipe in self.pipes[:]:
                pipe.update()
                
                # 检查碰撞
                if self.game_state == "playing":
                    bird_rect = self.bird.get_rect()
                    if (bird_rect.colliderect(pipe.get_top_rect()) or 
                        bird_rect.colliderect(pipe.get_bottom_rect())):
                        self.game_state = "game_over"
                        
                # 计分
                if not pipe.passed and pipe.x + pipe.width < self.bird.x:
                    pipe.passed = True
                    self.score += 1
                    
                # 移除屏幕外的管道
                if pipe.is_off_screen():
                    self.pipes.remove(pipe)
                    
    def draw_hell_background(self):
        # 绘制地狱背景渐变
        for y in range(SCREEN_HEIGHT):
            # 从深红到黑色的渐变
            ratio = y / SCREEN_HEIGHT
            r = int(139 + (0 - 139) * ratio)
            g = int(0 + (0 - 0) * ratio)
            b = int(0 + (0 - 0) * ratio)
            color = (max(0, min(255, r)), max(0, min(255, g)), max(0, min(255, b)))
            pygame.draw.line(self.screen, color, (0, y), (SCREEN_WIDTH, y))
        
        # 绘制背景粒子（火焰效果）
        for particle in self.background_particles:
            pygame.draw.circle(self.screen, FIRE_ORANGE, 
                             (int(particle['x']), int(particle['y'])), 
                             particle['size'])
    
    def draw_start_screen(self):
        # 绘制开始界面
        self.draw_hell_background()
        
        # 标题
        title_text = self.big_font.render("HELL FLAPPY BIRD", True, FIRE_ORANGE)
        title_rect = title_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 - 100))
        self.screen.blit(title_text, title_rect)
        
        # 开始按钮
        button_rect = pygame.Rect(SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 - 20, 200, 50)
        pygame.draw.rect(self.screen, DARK_RED, button_rect)
        pygame.draw.rect(self.screen, HELL_RED, button_rect, 3)
        
        start_text = self.font.render("START GAME", True, WHITE)
        start_rect = start_text.get_rect(center=button_rect.center)
        self.screen.blit(start_text, start_rect)
        
        # 说明文字
        instruction_text = self.font.render("Press SPACE or UP to start", True, WHITE)
        instruction_rect = instruction_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 + 60))
        self.screen.blit(instruction_text, instruction_rect)
        
        # 绘制演示小鸟
        demo_bird = Bird(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 + 120)
        demo_bird.draw(self.screen)
    
    def draw(self):
        if self.game_state == "start":
            self.draw_start_screen()
        else:
            # 绘制地狱背景
            self.draw_hell_background()
            
            # 绘制管道
            for pipe in self.pipes:
                pipe.draw(self.screen)
                
            # 绘制小鸟
            if self.game_state == "playing":
                self.bird.draw(self.screen)
                
            # 绘制分数
            score_text = self.font.render(f"Score: {self.score}", True, WHITE)
            self.screen.blit(score_text, (10, 10))
            
            # 绘制游戏结束信息
            if self.game_state == "game_over":
                game_over_text = self.big_font.render("GAME OVER!", True, FIRE_ORANGE)
                text_rect = game_over_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 - 50))
                self.screen.blit(game_over_text, text_rect)
                
                restart_text = self.font.render("Press SPACE or UP to restart", True, WHITE)
                restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2 + 20))
                self.screen.blit(restart_text, restart_rect)
                
        pygame.display.flip()
        
    def run(self):
        running = True
        while running:
            running = self.handle_events()
            self.update()
            self.draw()
            self.clock.tick(FPS)
            
        pygame.quit()
        sys.exit()

if __name__ == "__main__":
    game = Game()
    game.run()
